package org.vectrics.common.html;

import org.apache.ecs.html.Font;
import java.awt.Color;
import java.util.*;


public class TextHelper {

    public static Font getFont(String text) {
        Font font = new Font();
        font.addElement(text);
        return(font);
    };

    public static Font getFont(String className, String text) {
        Font font = new Font();
        font.setClass(className);
        if (text == null)
        	font.addElement("&nbsp;");
        else
        	font.addElement(text);
        return(font);
    };

    public static Font getFont(String className, Color color, String text) {
        Font font = new Font();
        font.setClass(className);
        font.setColor("#" + color.toString());
        font.addElement(text);
        return(font);
    };

    public static String escapeHtml(String s)
    {
       StringBuffer sb = new StringBuffer();
       int n = s.length();
       for (int i = 0; i < n; i++) {
          char c = s.charAt(i);
          switch (c) {
             case '<': sb.append("&lt;"); break;
             case '>': sb.append("&gt;"); break;
             case '&': sb.append("&amp;"); break;
             case '"': sb.append("&quot;"); break;
             default:  sb.append(c); break;
          }
       } return sb.toString();
    }


    public static String colorToString(Color color)   {
        String redStr = Integer.toHexString(color.getRed());
        while (redStr.length() < 2)
            redStr = "0" + redStr;
        String greenStr = Integer.toHexString(color.getGreen());
        while (greenStr.length() < 2)
            greenStr = "0" + greenStr;
        String blueStr = Integer.toHexString(color.getBlue());
        while (blueStr.length() < 2)
            blueStr = "0" + blueStr;
        return("#" + redStr + greenStr + blueStr);
    }

    public static Font getFont(int size, Color color, String text) {
        Font font = new Font();
        font.setSize(size);
        font.setColor(colorToString(color));
        font.addElement(text);
        return(font);
    };

    public static String getHeader(int level, String text) {
        String header = "<h" + level + ">" + text + "</h" + level + ">";
        return(header);
    }
    

    public static String getHtmlParagraphs(String className, String rawText) {
        int i = 0;
        int posStart = 0;
        String text = "";
        String beforeText = "";
        if (rawText != null)
            beforeText = rawText;
        while (i < beforeText.length()) {
            if (beforeText.charAt(i) == '\n') {
                text += "<p class=\"" + className + "\">" + beforeText.substring(posStart, i);
                text += "</p>\n";
                posStart = i + 1;
            }
            i++;
        }
        
        if (posStart < beforeText.length()) {
            text += "<p class=\"" + className + "\">" + beforeText.substring(posStart, beforeText.length())
                    + "</p>\n"; 
        }
        
        return(text);
    }
    
    public static String getHtmlParagraph(Collection lines) {
        return(getHtmlParagraph(lines, -1));
    }
    
    public static String getHtmlParagraph(Collection lines, int maxLines) {
        int lineCount = 0;
        StringBuffer str = new StringBuffer();
        Iterator iter = lines.iterator();
        boolean firstPass = true;
        boolean done = false;
        while ((iter.hasNext()) && (done == false)) {
            if (firstPass == false)
                str.append("<br>");
            else
                firstPass = false;
            lineCount++;
            if ((maxLines <= 0) || (lineCount <= maxLines)) {
                str.append((String)iter.next());
            }
            else {
                str.append("<br><i>more</i>");
                done = true;
            }
        }
        
        return(str.toString());
    }

}